package good

import (
	"enjoy_wangyashuo/internal/model/mysql"
	"gorm.io/gorm"
)

// 商品属性表
type GoodsProperty struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20);comment:属性名称;"`
	GoodsId int64  `gorm:"type:int(11);comment:关联的商品ID"`
}
type GoodsPropertyValue struct {
	gorm.Model
	PropertyID int64  `gorm:"type:int;comment:关联的商品属性ID;"`
	Name       string `gorm:"type:varchar(255);comment:属性值名称;"`
}
type PropertyInfo struct {
	Property    []GoodsProperty
	PropertyVal []GoodsPropertyValue
}

// 查属性列表 商品id查询  返回属性和属性值
func (g *GoodsProperty) GetGoodsPropertyValByGid(gid []int64) (infoList PropertyInfo, err error) {
	var valList []GoodsProperty
	var gv []GoodsPropertyValue
	return infoList, mysql.WithMysql(func(db *gorm.DB) error {
		//先查询这个商品所有属性
		err = db.Where("goods_id in (?)", gid).Find(&valList).Error
		if err != nil {
			return err
		}

		//获取所有属性id
		var pIds []int64
		for _, val := range valList {
			pIds = append(pIds, int64(val.ID))

		}
		//查询所有属性值的列表
		err = db.Model(&gv).Where("property_id in(?)", pIds).Find(&gv).Error
		infoList = PropertyInfo{
			Property:    valList,
			PropertyVal: gv,
		}
		return nil
	})
}
